 
from email.mime.multipart import MIMEMultipart
from email.header import Header
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
import os
import time
import smtplib
# from common.common import srmdirFlder
 
import  os, os.path
import zipfile
import platform
#压缩测试报告
def srmdirFlder(dirpath):
   # print("原始文件夹路径：" + dirpath)
    output_name = f"{dirpath}.zip"
    parent_name = os.path.dirname(dirpath)
 #   print("压缩文件夹目录：", parent_name)
    zip = zipfile.ZipFile(output_name, "w", zipfile.ZIP_DEFLATED)
    # 多层级压缩
    for root, dirs, files in os.walk(dirpath):
        for file in files:
            if str(file).startswith("~$"):
                continue
            filepath = os.path.join(root, file)
       #     print("压缩文件路径：" + filepath)
            writepath = os.path.relpath(filepath, parent_name)
            zip.write(filepath, writepath)


from email.mime.text import MIMEText
# # 配置邮箱服务器信息
 
mail_postfix="qq.com" #发件箱的后缀# 登陆邮箱
sent=smtplib.SMTP()
sent.connect('smtp.qq.com',25)
mail_user="@qq.com" # 发送人邮箱地址
mail_pass = " " # 注意：这里不是密码，而应该填写授权码！！
sent.login(mail_user, mail_pass) # 登陆
 # 配置发件人、收件人信息
sender = '@qq.com'  # 发件人邮箱
# 编辑邮件内容
receivers = ['@qq.com','@qq.com'] # 收件人邮箱地址
 
def message_config():
    #content = MIMEText('TDR测试报告,注意查收') # 正文内容
    message = MIMEMultipart()  # 多个MIME对象
    # 构造附件1
    dirpath = r"./allure-report"
    srmdirFlder(dirpath)
    time.sleep(5)
    content = MIMEText(open('./allure-report.zip', 'rb').read(), 'base64', 'gb2312')
    content["Content-Type"] = 'application/octet-stream'
    content["Content-Disposition"] = 'attachment; filename="testReport.zip"'  # 这里的filename可以任意写，写什么名字，邮件中显示什么名字
    message.attach(content)  # 添加内容
    message['Subject'] = '接口测试报告' # 邮件标题
    message['From'] = mail_user # 发件人
    message['To'] =','.join(receivers) #收件人，用逗号连接多个邮件，实现群发
    return message
 
def send_mail(message):
       try:
          sent.sendmail(mail_user, receivers, message.as_string())  #3个参数 发送人，收件人，邮件内容
          print('Success')
          sent.close()
       except smtplib.SMTPException:
          print("Error：Fail")
 
 
if __name__ == "__main__":
    print("开始执行")
    message = message_config()  # 调用配置方法
    send_mail(message)  # 发送邮件
    print("执行结束")
 